#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <unistd.h>
#include "log.hpp"
Log log;

class Task
{
public:
    Task(int sockfd, uint16_t port, std::string ip)
        : sockfd_(sockfd), port_(port), ip_(ip)
    {
    }
    ~Task()
    {
    }

    void run()
    {
        // 接收数据
        char buf[1024] = {0};
        int n = read(sockfd_, buf, sizeof(buf));
        if (n < 0)
        {
            log(Warning, "read fail");
        }
        else if (n == 0)
        {
            cout << "quit" << endl;
        }
        else
        {
            buf[n] = 0;
            std::cout << "server get msg:" << buf << std::endl;
            // 处理数据
            std::string msg = "server say: I have get msg >_";
            msg += buf;
            write(sockfd_, msg.c_str(), msg.size());
        }
    }
    void operator()()
    {
        run();
    }

private:
    int sockfd_;
    uint16_t port_;
    std::string ip_;
};